package com.lsp.lib_uikit.commonrecycler.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lsp
 *         created 16/1/21 15:13
 */
public abstract class BasicRvAdapter<D> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private int oldItemPosition = -1;

  protected LayoutInflater mLayoutInflater;
  protected Context mContext;
  /**
   * 绑定到列表的数据集合
   */
  private final List<D> mList;

  public BasicRvAdapter(Context mContext) {
    this.mContext = mContext;
    this.mLayoutInflater = LayoutInflater.from(mContext);
    this.mList = new ArrayList<>();
  }

  public BasicRvAdapter(Context context, List<D> list) {
    this.mContext = context;
    this.mLayoutInflater = LayoutInflater.from(context);
    this.mList = list;
  }

  /**
   * @param isClear 是否清除原list数据
   * 刷新数据
   */
  public void addAllAndNotifyChanged(List<? extends D> list, boolean isClear) {
    if (isClear) {
      this.mList.clear();
    }
    if (list == null) {
      list = new ArrayList<>();
    }
    this.mList.addAll(list);
    notifyDataSetChanged();
  }

  //public void notifyAllDataOnSafe(List<? extends D> list, boolean isClear) {
  //  if (list != null) {
  //    int previousSize = mList.size();
  //    if (isClear) {
  //      this.mList.clear();
  //      notifyItemRangeRemoved(0, previousSize);
  //    }
  //    if (list == null) {
  //      list = new ArrayList<>();
  //    }
  //    int startPosition = mList.size();
  //    this.mList.addAll(list);
  //    notifyItemRangeInserted(startPosition, startPosition + list.size());
  //  }
  //}

  /**
   * 获取所有的列表数据<BR>
   *
   * @return 返回所有的集合数据
   */
  public List<D> getAllList() {
    return mList;
  }

  /**
   * itemcount  要把 header  footer计入
   */
  @Override public int getItemCount() {
    return this.mList == null ? 0 : mList.size();
  }

  @Override public int getItemViewType(int position) {

    return getDefItemViewType(position);
  }

  /**
   * 列表条目对应的viewtype
   * 可以通过重写该方法来获取对应的viewtype
   */
  public abstract int getDefItemViewType(int position);

  @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder commonHolder = onCreateDefViewHolder(parent, viewType);
    initItemClickListener(commonHolder);
    return commonHolder;
  }

  /**
   * 重写该方法获取对应item
   */
  public abstract RecyclerView.ViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType);

  @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    onDefBindViewHolder(holder, position);
    //addAnimation(holder);
  }

  /**
   * 重写onDefBindViewHolder 绑定数据  代替{@link #onBindViewHolder}
   */
  protected abstract void onDefBindViewHolder(RecyclerView.ViewHolder holder, int position);

  /**
   * 刷新单个itemm
   *
   * @param currentSelect 当前选中position
   */
  public void refreshItem(int currentSelect) {
    if (oldItemPosition != -1) {
      notifyItemChanged(oldItemPosition);
    }
    oldItemPosition = currentSelect;
    notifyItemChanged(currentSelect);
  }

  @Override public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    holder.itemView.clearAnimation();
  }

  /**
   * =================监听==============start================
   */

  private void initItemClickListener(final RecyclerView.ViewHolder commonHolder) {

    if (mOnItemClickListener != null) {
      commonHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          mOnItemClickListener.onItemClick(v, commonHolder.getLayoutPosition());
        }
      });
    }

    if (onRecyclerViewItemLongClickListener != null) {
      commonHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override public boolean onLongClick(View v) {
          return onRecyclerViewItemLongClickListener.onItemLongClick(v,
              commonHolder.getLayoutPosition());
        }
      });
    }
  }

  private OnItemClickListener mOnItemClickListener;
  private OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener;

  /**
   * Register a callback to be invoked when an item in this AdapterView has
   * been clicked
   */
  public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
    this.mOnItemClickListener = mOnItemClickListener;
  }

  /**
   * Register a callback to be invoked when an item in this AdapterView has
   * been clicked and held
   *
   * @param onRecyclerViewItemLongClickListener The callback that will run
   */
  public void setOnRecyclerViewItemLongClickListener(
      OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener) {
    this.onRecyclerViewItemLongClickListener = onRecyclerViewItemLongClickListener;
  }

  /**
   * Interface definition for a callback to be invoked when an item in this
   * view has been clicked
   */
  public interface OnItemClickListener {
    void onItemClick(View view, int position);
  }

  /**
   * Interface definition for a callback to be invoked when an item in this
   * view has been clicked and held
   */
  public interface OnRecyclerViewItemLongClickListener {
    /**
     * callback method to be invoked when an item in this view has been
     * click and held
     *
     * @param view The view whihin the AbsListView that was clicked
     * @param position The position of the view int the adapter
     * @return true if the callback consumed the long click ,false otherwise
     */
    boolean onItemLongClick(View view, int position);
  }
}
